package com.dengyiming.user.web;

import com.dengyiming.user.pojo.User;
import com.dengyiming.user.properties.JwtProperties;
import com.dengyiming.user.service.IUserService;
import com.dengyiming.user.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("list")
    public String userList(@RequestParam("username") String username,
                           @RequestParam("password") String password) {

        User user = userService.userList(username, password);
        if (user != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", user.getId());
            String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),
                    jwtProperties.getUserTtl(),
                    claims);
            log.info("JWT令牌：{}",token);
            return token;
        }
        return "登录失败";
    }
}
